package com.imgo.max.fans.service;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.imgo.max.artist.constant.FansMQTagConstant;
import com.imgo.max.artist.dto.CancelCollectHisDto;
import com.imgo.max.artist.service.FansMessageService;
import com.imgo.max.common.dto.ResultCodeEnum;
import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.log.CustomLogger;
import com.imgo.max.common.log.CustomLoggerFactory;
import com.imgo.max.common.result.ModuleResultCode;
import com.imgo.max.common.util.LoggerUtils;
import com.imgo.max.common.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 粉丝增、删、改
 * 
 * @author darren.dai
 */
@Service("fansMessageService")
public class FansMessageServiceImpl implements FansMessageService {
	private static final CustomLogger logger = CustomLoggerFactory.getLogger(FansMessageServiceImpl.class);

	@Resource
	ProducerBean producer;

	String topic = null;
	/**
	 * fans topic
	 */
	private String getTopic() {
		if (com.imgo.max.core.tools.StringUtils.isNotBlank(topic)) {
			return topic;
		}
		topic = (String) producer.getProperties().get("topic");
		return topic;
	}
	/**
	 * 用户取消艺人关注
	 * 
	 * @param uid
	 * @param artistUid
	 */
	@Override
	public ResultDO cancelCollectArtist(String uid, String artistUid) {
		ResultDO resultDO = ResultDO.buildResult();
		// 参数为空
		if (StringUtils.isBlank(uid) || StringUtils.isBlank(artistUid)) {
			return new ResultDO(ResultCodeEnum.MISSING_PARAMS, false);
		}

		try {
			String tmpStr = uid + ":" + artistUid;
			Message message = new Message(getTopic(), FansMQTagConstant.TAG_CANCEL_COLLECT_ARTIST, tmpStr.getBytes());

			SendResult send = producer.send(message);
			logger.info("send message Id:{}", send.getMessageId());
		} catch (Exception e) {
			LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid, artistUid);
		}

		return resultDO;
	}

	/**
	 * 取消用户关注所有艺人
	 */
	@Override
	public ResultDO cancelCollectArtists(String uid) {
		ResultDO resultDO = ResultDO.buildResult();
		if(StringUtils.isBlank(uid)){
			resultDO.setResultCode(ResultCodeEnum.MISSING_PARAMS.getResultCode());
			resultDO.setSuccess(true);
			return resultDO;
		}
		try {
			Message message = new Message(getTopic(), FansMQTagConstant.TAG_CANCEL_COLLECT_ALL_ARTISTS, uid.getBytes());
			SendResult send = producer.send(message);
			logger.info("send message Id:{}", send.getMessageId());
		} catch (Exception e) {
			LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid);
		}
		return resultDO;
	}

	/**
	 * 关注艺人
	 */
	@Override
	public ResultDO collectArtist(String uid, String artistId) {
		ResultDO resultDO = new ResultDO<>();
		if(StringUtils.isBlank(uid) || StringUtils.isBlank(artistId)){
			resultDO.setResultCode(ResultCodeEnum.MISSING_PARAMS.getResultCode());
			resultDO.setSuccess(true);
			return resultDO;
		}
		try {
			String tmpStr = uid + ":" + artistId;
			Message message = new Message(getTopic(), FansMQTagConstant.TAG_COLLECT_ARTISTS, tmpStr.getBytes());
			SendResult send = producer.send(message);
			logger.info("send message Id:{}", send.getMessageId());
		} catch (Exception e) {
			LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid, artistId);
		}
		return resultDO;
	}

	/**
	 * 取消关注记录到历史表中
	 */
	@Override
	public ResultDO insertCancelCollectHisByDto(CancelCollectHisDto cancelCollectHisDto) {
		ResultDO resultDO = new ResultDO<>();
		try {
            byte[] bytes = ObjectUtil.toByteArray(cancelCollectHisDto);
            Message message = new Message(getTopic(), FansMQTagConstant.TAG_INSERT_CANCEL_COLLECT_HISTORY, bytes);
            SendResult send = producer.send(message);
            resultDO.setModule(1);
        } catch (Exception e) {
			LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, JSON.toJSONString(cancelCollectHisDto));
        }
        return resultDO;
	}

}
